/**
 * 权限指令
 * 用于控制按钮级别的权限
 * 使用方式：v-permission="'system:menu:add'"
 */

import store from '../store'

export default {
  mounted(el, binding) {
    const { value } = binding
    const permissions = store.state.permissions

    if (value && typeof value === 'string') {
      const hasPermission = permissions.includes(value)

      if (!hasPermission) {
        el.parentNode && el.parentNode.removeChild(el)
      }
    } else if (value && Array.isArray(value) && value.length > 0) {
      const hasPermission = value.some(permission => {
        return permissions.includes(permission)
      })

      if (!hasPermission) {
        el.parentNode && el.parentNode.removeChild(el)
      }
    } else {
      throw new Error('需要指定权限标识')
    }
  }
}